let db = require("../db/index");

// 车位管理列表查询
exports.garaglist = (req, res) => {
    var sql =
        `SELECT * FROM garage,parkinglot where garage.GargrID=parkinglot.GargrID `;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};
// 车位管理列表查询(根据date)
// exports.garaglistselect = (req, res) => {
//     console.log(req.body)
//     var sql =
//         `select * from garagemanagement where date=${req.query.date}`;
//     db.query(sql, [], (err, data) => {
//         if (err) {
//             console.log("出错了", err);
//             return res.send("错误" + err.message);
//         }else{
//             res.send(data);
//         }
        
//     });
// };
// 添加车位数据
exports.garaglistinsert = (req, res) => {
    console.log(req.body)
    var sql =
        `SELECT GargrID from garage WHERE Belongingcommunity="万象城" and GargeName="地下车库" and GargeErea="A区" `;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `INSERT INTO parkinglot(ParkinglotNumber,Housingstatus,ParkinglotErea,GargrID,ParkinglotNote,Housetype,edit)VALUES("${req.body.ParkinglotNumber}","${req.body.Housingstatus}","${req.body.ParkinglotErea}",${data[0].GargrID},"${req.body.ParkinglotNote}","车位","绑定业主")`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send({
                        data,
                        state:200
                    });
                }
                
            });
        }
    });
    
};
// 修改车位数据
exports.garaglistupdate = (req, res) => {
    console.log(req.body)
    var sql =
        `UPDATE parkinglot set ParkinglotNumber="${req.body.ParkinglotNumber}",Housingstatus="${req.body.Housingstatus}",ParkinglotErea="${req.body.ParkinglotErea}",GargrID=${req.body.GargrID},ParkinglotNote="${req.body.ParkinglotNote}" WHERE date=${req.body.ParkinglotID}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            // res.send(data);
            var sql =
                `UPDATE garage set Belongingcommunity="${req.body.Belongingcommunity}",GargeName="${req.body.GargeName}",GargeErea="${req.body.GargeErea}" WHERE GargrID=${req.body.GargrID}`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send({
                        data,
                        state:200
                    });
                }
                
            });
        }
        
    });
};
// 删除车位数据某一位
exports.garaglistdelete = (req, res) => {
    console.log(req.body)
    var sql =
        `delete from parkinglot where date=${req.body.date}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM garage,parkinglot where garage.GargrID=parkinglot.GargrID `;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data);
                }
            });
        }
        
    });
};

// 车库管理数据
exports.carportlist = (req, res) => {
    var sql =
        `select * from garage `;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};



// 根据所需字段查询
// exports.carportlist = (req, res) => {
//     var sql =
//         `select date,Belongingcommunity,Garagename,ParkingArea,notegarge from garagemanagement `;
//     db.query(sql, [], (err, data) => {
//         if (err) {
//             console.log("出错了", err);
//             return res.send("错误" + err.message);
//         }
//         res.send(data);
//     });
// };
// 添加车库管理数据
exports.carportlistinsert = (req, res) => {
    var sql =
        `insert into garage(Belongingcommunity,GargeName,GargeErea,GargeNote) values("${req.body.Belongingcommunity}","${req.body.GargeName}","${req.body.GargeErea}","${req.body.GargeNote}")`;
    db.query(sql, [], (err, data) => {
        console.log(sql)
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `select * from garage `;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send(data);
            });
        }
        
    });
};
// 更新车库数据
exports.carportlistupdate = (req, res) => {
    var sql =
        `UPDATE garage set Belongingcommunity="${req.body.Belongingcommunity}",GargeName="${req.body.GargeName}",GargeErea="${req.body.GargeErea}",GargeNote="${req.body.GargeNote}" WHERE GargrID=${req.body.GargrID}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send({
                data,
                state:200
            });
        }
        
    });
};
// 删除车库数据
exports.carportlistdelete = (req, res) => {
    console.log(req.body)
    var sql =
        `delete from garage where GargrID=${req.body.date}`;
    db.query(sql, [], (err, data) => {
        console.log(sql)
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            // res.send(data);
            var sql =
                `SELECT * FROM garage`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data);
                }
            });
            var sql =
                `DELETE FROM parkinglot WHERE GargrID=${req.body.date}`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    
                }
            });
        }
        
    });
};

// 根据业主id查询车位
exports.parkinglotselect = (req, res) => {
    var sql =
        `SELECT * FROM parkinglot WHERE housedate=${req.body.date}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send({
                data,
                state:200
            });
        }
        
    });
};